﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;
using Microsoft.Win32;


namespace WEB.T_all
{
    public partial class YSshou : System.Web.UI.Page
    {
        T_AllManage tALL = new T_AllManage();
        SheetInfoManage SM = new SheetInfoManage();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        Shou_Excel dataToExcel = new Shou_Excel();
        CustomerManage CM = new CustomerManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime A = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);
                int month = Convert.ToInt32(A.Date.Month.ToString());
                tbGency1.Text = A.ToShortDateString();
                tbGency.Text = A.AddMonths(-2).Date.ToShortDateString();
            }
                string Cname = tbCus.Text;
            
                ds = tALL.GetList1(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), Cname);
                gvTALL.DataSource = ds;
                gvTALL.DataBind();
                if (gvTALL.Rows.Count == 0)
                {
                    Label5.Text = "没有数据";
                }
                else
                {
                    Label5.Text = "";
                }


            this.tbGency.Attributes.Add("onClick", "new WdatePicker()");
            this.tbGency1.Attributes.Add("onClick", "new WdatePicker()");
        }

        protected void tbGency_TextChanged(object sender, EventArgs e)
        {

        }

        protected void tbGency1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindData();
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }

        private void BindData()
        {
            
            DateTime KAI, MO;
            if (tbGency.Text == "")
            {
                KAI = Convert.ToDateTime("1900-1-1").Date;
            }
            else
            {
                KAI = Convert.ToDateTime(tbGency.Text).Date;
            }

            if (tbGency1.Text == "")
            {
                MO = Convert.ToDateTime("2099-1-1").Date;
            }

            else
                MO = Convert.ToDateTime(tbGency1.Text).Date;

            tbGency.Text = Convert.ToString(KAI);
            tbGency1.Text = Convert.ToString(MO);

            string Cname = tbCus.Text;

            ds = tALL.GetList1(KAI, MO, Cname);
            gvTALL.DataSource = ds;
            gvTALL.DataBind();

            //空格赋值
            decimal money = 0, a, b;
            int i;
            decimal sum1 = 0, sum2 = 0, sum3 = 0;
            for (i = 0; i < gvTALL.Rows.Count; i++)
            {
                if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                    a = 0;
                else
                    a = Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                    b = 0;
                else
                    b = Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);

                money = a - b;
                gvTALL.Rows[i].Cells[8].Text = money.ToString();
            }
            //求和
            for (i = 0; i < gvTALL.Rows.Count; i++)
            {
                if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                    sum1 = sum1 + 0;
                else
                    sum1 = sum1 + Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                    sum2 = sum2 + 0;
                else
                    sum2 = sum2 + Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);
                if (gvTALL.Rows[i].Cells[8].Text == "&nbsp;")
                    sum3 = sum3 + 0;
                else
                    sum3 = sum3 + Convert.ToDecimal(gvTALL.Rows[i].Cells[8].Text);
            }
            //显示和
            Label1.Text = sum1.ToString();
            Label2.Text = sum2.ToString();
            Label3.Text = sum3.ToString();
        }

        protected void gvTALL_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            decimal money = 0, a, b;
            int i;
            decimal sum1 = 0, sum2 = 0, sum3 = 0;
            //给空格赋值
            for (i = 0; i < gvTALL.Rows.Count; i++)
            {
                if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                {
                    gvTALL.Rows[i].Cells[6].Text = "0";
                    a = 0;
                }
                else
                    a = Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                {
                    gvTALL.Rows[i].Cells[7].Text = "0";
                    b = 0;
                }
                else
                    b = Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);

                money = a - b;
                gvTALL.Rows[i].Cells[8].Text = money.ToString();
            }
            //求和
            for (i = 0; i < gvTALL.Rows.Count; i++)
            {
                if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                    sum1 = sum1 + 0;
                else
                    sum1 = sum1 + Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                    sum2 = sum2 + 0;
                else
                    sum2 = sum2 + Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);
                if (gvTALL.Rows[i].Cells[8].Text == "&nbsp;")
                    sum3 = sum3 + 0;
                else
                    sum3 = sum3 + Convert.ToDecimal(gvTALL.Rows[i].Cells[8].Text);
            }
            //显示和
            Label1.Text = sum1.ToString();
            Label2.Text = sum2.ToString();
            Label3.Text = sum3.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSfu.aspx");
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (gvTALL.Rows.Count > 0)
            {
                DateTime KAI, MO;
                if (tbGency.Text == "")
                {
                    KAI = Convert.ToDateTime("1900-1-1").Date;
                }
                else
                {
                    KAI = Convert.ToDateTime(tbGency.Text).Date;
                }

                if (tbGency1.Text == "")
                {
                    MO = Convert.ToDateTime("2099-1-1").Date;
                }
                else
                    MO = Convert.ToDateTime(tbGency1.Text).Date;

                string Cname = tbCus.Text;
                //取得列名
                ds = tALL.GetList1(KAI, MO, Cname);
                foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    dt.Columns.Add(dc.ColumnName, dc.DataType);
                }
                //读取数据
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    object[] arrdr = new object[ds.Tables[0].Columns.Count];
                    ds.Tables[0].Rows[i].ItemArray.CopyTo(arrdr, 0);
                    dt.Rows.Add(arrdr);
                }

                //生成excel
                string FilePath = Server.MapPath("~/document/Excel/");
                string file = dataToExcel.OutputExcel(dt, Cname, KAI, MO, "应收实收", FilePath);
                DownloadFile(file);
                return;
            }
            else
            {
                Response.Write("<script>alert('没有该客户的任何数据，请核查!')</script>");
                return;
            }
        }
        protected void DownloadFile(string file)
        {
            string filename = "/document/Excel/" + file;
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            //对文件进行编码，防止下载名出现乱码
            string encodefileName = System.Web.HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8);

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();
        }

        //public DataTable Getdatable(DateTime Kai,DateTime Mo,string user)
        //{
        //    //通过sheetID取得dt
        //    dt = tALL.Shou_GetView("a.sheetID in (" + x + ")").Tables[0];
        //    return dt;
        //}

        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }
        /// <summary>
        /// 跳转利润统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        #region 页面跳转
        protected void btLirun_Click(object sender, EventArgs e)
        {
            Response.Redirect("Lirun.aspx");
        }

        protected void btWF_Click(object sender, EventArgs e)
        {
            Response.Redirect("FeeWF.aspx");
             
        }

        protected void YSfu_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSfu.aspx");
        }

        protected void btWS_Click(object sender, EventArgs e)
        {
            Response.Redirect("FeeWS.aspx");
        }
        #endregion

        protected void gvTALL_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTALL.PageIndex = e.NewPageIndex;
            gvTALL.DataBind();
        }
    }
}